﻿using Abp;
using Abp.Localization;
using Abp.MultiTenancy;
using Abp.Authorization;
using Abp.Configuration.Startup;
using GCT.Foundation.ApkManagement.Authorization;

namespace GCT.Foundation.Authorization
{
    public class TemplateAuthorizationProvider : AuthorizationProvider
    {
        private readonly bool _isMultiTenancyEnabled;

        public TemplateAuthorizationProvider(bool isMultiTenancyEnabled)
        {
            _isMultiTenancyEnabled = isMultiTenancyEnabled;
        }

        public TemplateAuthorizationProvider(IMultiTenancyConfig multiTenancyConfig)
        {
            _isMultiTenancyEnabled = multiTenancyConfig.IsEnabled;
        }

        public override void SetPermissions(IPermissionDefinitionContext context)
        {
            //Host permissions
            //只有当宿主登陆后才能管理的权限
            //var tenants = administration.CreateChildPermission(PermissionNames.Pages_Tenants, L("Tenants"), multiTenancySides: MultiTenancySides.Host);


            // 顶级公共权限-页面
            var pages = context.GetOrCreatePermission(AppPermissions.Pages, L("Pages"));

            #region 工作台

            // 宿主-工作台
            pages.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Host_Dashboard,
                L("Dashboard"), multiTenancySides: MultiTenancySides.Host);

            // 租户-工作台
            pages.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Tenant_Dashboard, L("Dashboard"),
                multiTenancySides: MultiTenancySides.Tenant);

            #endregion



            #region Saas

            // Saas
            var saas = pages.GetOrCreateChildPermission(AppPermissions.Pages_Saas, L("Saas"), multiTenancySides: MultiTenancySides.Host);

            // 租户管理
            var tenants = saas.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Tenants, L("Tenants"),
                multiTenancySides: MultiTenancySides.Host);
            tenants.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Tenants_Query, L("Query"),
                multiTenancySides: MultiTenancySides.Host);
            tenants.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Tenants_Create, L("Create"),
                multiTenancySides: MultiTenancySides.Host);
            tenants.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Tenants_Edit, L("Edit"),
                multiTenancySides: MultiTenancySides.Host);
            tenants.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Tenants_Delete, L("Delete"),
                multiTenancySides: MultiTenancySides.Host);
            tenants.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Tenants_BatchDelete, L("BatchDelete"),
                multiTenancySides: MultiTenancySides.Host);
            tenants.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Tenants_ChangeFeatures, L("ChangingFeatures"),
                multiTenancySides: MultiTenancySides.Host);
            tenants.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Tenants_Impersonation, L("LoginForTenants"),
                multiTenancySides: MultiTenancySides.Host);

            // 版本管理
            var editions = saas.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Editions, L("Editions"),
                multiTenancySides: MultiTenancySides.Host);
            editions.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Editions_Query, L("Query"),
                multiTenancySides: MultiTenancySides.Host);
            editions.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Editions_Create, L("Create"),
                multiTenancySides: MultiTenancySides.Host);
            editions.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Editions_Edit, L("Edit"),
                multiTenancySides: MultiTenancySides.Host);
            editions.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Editions_Delete, L("Delete"),
                multiTenancySides: MultiTenancySides.Host);
            editions.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Editions_MoveTenantsToAnotherEdition,
                L("MoveTenantsToAnotherEdition"), multiTenancySides: MultiTenancySides.Host);

            #endregion



            #region 系统权限

            // 系统权限
            var administration = pages.GetOrCreateChildPermission(AppPermissions.Pages_Administration, L("Administration"));

            // 组织机构
            var organizationUnits =
                administration.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_OrganizationUnits,
                    L("OrganizationUnits"));
            organizationUnits.GetOrCreateChildPermission(
                YoyoSoftPermissionNames.Pages_Administration_OrganizationUnits_Query, L("Query"));
            organizationUnits.GetOrCreateChildPermission(
                YoyoSoftPermissionNames.Pages_Administration_OrganizationUnits_ManageOrganizationTree,
                L("ManagingOrganizationTree"));
            organizationUnits.GetOrCreateChildPermission(
                YoyoSoftPermissionNames.Pages_Administration_OrganizationUnits_ManageUsers, L("ManagingMembers"));
            organizationUnits.GetOrCreateChildPermission(
                YoyoSoftPermissionNames.Pages_Administration_OrganizationUnits_ManageRoles, L("ManagingRoles"));


            // 角色
            var roles = administration.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Roles,
                L("Roles"));
            roles.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Roles_Query, L("Query"));
            roles.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Roles_Create,
                L("Create"));
            roles.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Roles_Edit, L("Edit"));
            roles.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Roles_Delete, L("Delete"));


            // 用户
            var users = administration.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Users,
                L("Users"));
            users.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Users_Query, L("Query"));
            users.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Users_Create,
                L("Create"));
            users.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Users_Edit, L("Edit"));
            users.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Users_Delete, L("Delete"));
            users.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Users_BatchDelete,
                L("BatchDelete"));
            users.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Users_ChangePermissions,
                L("ChangingPermissions"));
            users.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Users_Impersonation,
                L("LoginForUsers"));

            users.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Users_ResetPassword, L("ResetPassword"));
            users.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Users_Unlock, L("Unlock"));


            users.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Users_ImportExcel, L("ImportFromExcel"));
            users.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Users_ExportToExcel, L("ExportToExcel"));
            #endregion



            #region 系统管理

            var systemManager = pages.GetOrCreateChildPermission(AppPermissions.Pages_SystemManager, L("SystemManager"));

            // 语言管理
            var languages = systemManager.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Languages,
                L("Languages"));
            languages.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Languages_Query, L("Query"));
            languages.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Languages_Create,
                L("Create"));
            languages.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Languages_Edit,
                L("Edit"));
            languages.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Languages_Delete,
                L("Delete"));
            languages.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Languages_ChangeTexts,
                L("ChangingTexts"));

            // APK管理
            var apkManagement =
                systemManager.GetOrCreateChildPermission(ApkManagementPermissions.ApkManagement_Node, L("ApkManagement"));
            apkManagement.GetOrCreateChildPermission(ApkManagementPermissions.ApkManagement_Query, L("Query"));
            apkManagement.GetOrCreateChildPermission(ApkManagementPermissions.ApkManagement_Create,
                L("Create"));
            apkManagement.GetOrCreateChildPermission(ApkManagementPermissions.ApkManagement_Edit, L("Edit"));
            apkManagement.GetOrCreateChildPermission(ApkManagementPermissions.ApkManagement_Delete,
                L("Delete"));
            apkManagement.GetOrCreateChildPermission(ApkManagementPermissions.ApkManagement_BatchDelete,
                L("BatchDelete"));
            apkManagement.GetOrCreateChildPermission(ApkManagementPermissions.ApkManagement_ExportExcel, L("ExportToExcel"));



            // 审计日志-包含api日志与数据修改日志
            systemManager.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_AuditLogs,
                L("AuditLogs"));

            // Hangfire面板
            systemManager.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_HangfireDashboard,
                L("HangfireDashboard"),
                multiTenancySides: _isMultiTenancyEnabled ? MultiTenancySides.Host : MultiTenancySides.Tenant);




            // 动态菜单
            systemManager.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Dynamic_Menu, L("DynamicMenu"));

            // 维护与日志
            systemManager.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Host_Maintenance,
                L("Maintenance"),
                multiTenancySides: _isMultiTenancyEnabled ? MultiTenancySides.Host : MultiTenancySides.Tenant);
            // 弃用的
            //systemManager.GetOrCreateChildPermission(
            //    YoyoSoftPermissionNames.Pages_Administration_MaintainAndAuditSettingSetting, L("MaintenanceAndLog"),
            //    multiTenancySides: _isMultiTenancyEnabled ? MultiTenancySides.Host : MultiTenancySides.Tenant
            //);

            // 设置-Host 
            systemManager.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Host_Settings,
                L("Settings"), multiTenancySides: MultiTenancySides.Host);

            // 设置-Tenant
            systemManager.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Tenant_Settings,
                L("Settings"), multiTenancySides: MultiTenancySides.Tenant);

            // 许可证设置
            systemManager.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_LicenseSetting,
                L("LicenseSetting"));


            // 文件管理
            var fileManagement = systemManager.GetOrCreateChildPermission(SysFilePermissions.Node, L("SysFile"));
            fileManagement.GetOrCreateChildPermission(SysFilePermissions.Query, L("Query"));
            fileManagement.GetOrCreateChildPermission(SysFilePermissions.Create, L("Create"));
            fileManagement.GetOrCreateChildPermission(SysFilePermissions.Edit, L("Edit"));
            fileManagement.GetOrCreateChildPermission(SysFilePermissions.Delete, L("Delete"));
            fileManagement.GetOrCreateChildPermission(SysFilePermissions.BatchDelete, L("BatchDelete"));
            fileManagement.GetOrCreateChildPermission(SysFilePermissions.ExportExcel, L("ExportToExcel"));


            // 在线用户
            var onlineUser = systemManager.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Users_Onine, L("OnlineUser"));
            onlineUser.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Users_Downline, L("Downline"));

            // 订阅-Tenant
            // pages.GetOrCreateChildPermission(YoyoSoftPermissionNames.Pages_Administration_Tenant_SubscriptionManagement,
            //     L("Subscription"), multiTenancySides: MultiTenancySides.Tenant);

            #endregion

            // 演示功能
            pages.GetOrCreateChildPermission(AppPermissions.Pages_DemoFunction, L("DemoFunction"));

        }



        private static ILocalizableString L(string name)
        {
            return new LocalizableString(name, AppConsts.LocalizationSourceName);
        }
    }
}
